export function pickRandomArrayItem<T>(array: T[]): T {
  return array[Math.floor(Math.random() * array.length)];
}

export function getFriendlyNumberText(i: number) {
  if (Number.isInteger(i) || isNaN(i)) return i;
  return i.toFixed(2);
}

export function escapeStringRegexp(string: string) {
  if (typeof string !== 'string') {
    throw new TypeError('Expected a string');
  }

  // Escape characters with special meaning either inside or outside character sets.
  // Use a simple backslash escape when it’s always valid, and a `\xnn` escape when the simpler form would be disallowed by Unicode patterns’ stricter grammar.
  return string.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
}

export function convertProjectionToArray<T extends string>(
  projection: Partial<Record<T, 0 | 1>>,
): T[] {
  return Object.keys(projection).filter((name) => projection[name]) as T[];
}
